package com.example.donghua;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        intiView();
        intiDemo();

    }
    private void intiView(){
        ImageView imageView =findViewById(R.id.image_anim);
        Button startButton=findViewById(R.id.but_start);
        Button endButton=findViewById(R.id.but_end);
        AnimationDrawable anim=(AnimationDrawable)imageView.getBackground();
        startButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.start();
            }
        });

        endButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                anim.stop();
            }
        });
    }

    private void intiDemo(){
        ImageView alphaImage=findViewById(R.id.image_anim_bu);
        //所有补间动画的父类为Animation
        Animation alpha= AnimationUtils.loadAnimation(this,R.anim.anim_alpha);
        alphaImage.setAnimation(alpha);
        //缩放动画
        ImageView  scaleImage=findViewById(R.id.image_suo);
        scaleImage.setAnimation(AnimationUtils.loadAnimation(this,R.anim.anim_scale));
        //位移动画
        ImageView  translateImage=findViewById(R.id.image_yi);
        translateImage.setAnimation(AnimationUtils.loadAnimation(this,R.anim.anim_trans));
        //所有动画都有对应的类，可以通过JAVA直接new出动画对象
        TranslateAnimation translateAnimation=new TranslateAnimation(0,0,200,200);
        translateAnimation.setFillAfter(true);
        //旋转动画
        ImageView imageView=findViewById(R.id.image_xuan);
        imageView.setAnimation(AnimationUtils.loadAnimation(this,R.anim.anim_rotate));
    }
}